--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type BP_PlatformChar_C
local M = UnLua.Class()

-- function M:Initialize(Initializer)
-- end

-- function M:UserConstructionScript()
-- end

function M:ReceiveBeginPlay()
    self.GameMode = UE.UGameplayStatics.GetGameMode(self):Cast(GameModeClass)
end

-- function M:ReceiveEndPlay()
-- end

-- function M:ReceiveTick(DeltaSeconds)
-- end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

-- 攻击
function M:Attack()
    self:GetAnimInstance():JumpToNode("JumpAttack")

    local Class = UE.UClass.Load("/Script/Engine.Blueprint'/TowerClimber/Blueprints/Characters/BP_PlatformChar.BP_PlatformChar_C'")

    local HitResults = UE.TArray(UE.FHitResult)

    local StartLocation = self.AttackBox:K2_GetComponentLocation()

    local Hit = UE.UKismetSystemLibrary.BoxTraceMultiForObjects(
            self,
            StartLocation,
            StartLocation,
            self.AttackBox.BoxExtent,
            UE.FRotator(),
            self.AttackCollisionType,
            false,
            UE.TArray(UE.AActor),
            UE.EDrawDebugTrace.None,
            HitResults,
            true,
            UE.FLinearColor(1, 0, 0, 1), UE.FLinearColor(0, 1, 0, 1), 0.1
    )
    if Hit then
        for i = 1, HitResults:Length() do
            local Character = HitResults:Get(i).HitObjectHandle.Actor:Cast(Class)
            Character:Hit()
            local Launch = UE.FVector()
            Launch = self:GetActorForwardVector() * 500 + self:GetActorUpVector() * 800
            Character:LaunchCharacter(Launch, true, true)
        end
    end
end

-- 被击中
function M:Hit()
    self:GetAnimInstance():JumpToNode("JumpHit")
end


-- 被击败
function M:Defeat()
    if self.IsDefeated == false then
        self.IsDefeated = true
        self.CapsuleComponent:SetCollisionEnabled(UE.ECollisionEnabled.NoCollision)
        --- 解除平面约束
        self.CharacterMovement:SetPlaneConstraintEnabled(false)
        self:LaunchOnDefeat()
        self:GetAnimInstance():JumpToNode("JumpDefeated")
        self.Sprite:SetTranslucentSortPriority(100)
    end

end

-- 失败时发射
function M:LaunchOnDefeat()
    local LaunchVelocity = UE.FVector()
    LaunchVelocity = UE.UKismetMathLibrary.Vector_Up() * self.DefeatUpLaunch + UE.UKismetMathLibrary.Vector_Right() * self.DefeatForwardLaunch
    self:LaunchCharacter(LaunchVelocity, true, true)
end

return M
